<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="礼品名称" prop="giftname">
      <el-input v-model="ruleForm.giftname" style="width: 200px" placeholder="请输入礼品名称"></el-input>
    </el-form-item>
    <el-form-item label="生效时间" prop="begintime">
      <el-date-picker format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                      v-model="ruleForm.begintime" type="datetime" style="width: 200px" placeholder="选择日期时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="失效时间" prop="endtime">
      <el-date-picker format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                      v-model="ruleForm.endtime" type="datetime" style="width: 200px" placeholder="选择日期时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="奖品等级" prop="prizelevel">
      <el-select v-model="ruleForm.prizelevel" placeholder="请选择礼品等级">
        <el-option label="一等奖" value="1"></el-option>
        <el-option label="二等奖" value="2"></el-option>
        <el-option label="三等奖" value="3"></el-option>
        <el-option label="四等奖" value="4"></el-option>
        <el-option label="五等奖" value="5"></el-option>
        <el-option label="六等奖" value="6"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="入库数量" prop="stocknum">
      <el-input v-model="ruleForm.stocknum" style="width: 200px" placeholder="请输入数量"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remarks">
      <el-input type="textarea" style="width: 200px" v-model="ruleForm.remarks"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import gift from "../../api/gift";

export default {
  data() {
    return {
      ruleForm: {
        prizelevel: '',//礼品等级
        stocknum: '',//礼品数量
        giftname: '',//礼品名称
        begintime: null,//生效时间
        endtime: null,//失效时间
        remarks: ''//备注
      },
      rules: {
        prizelevel: [
          {required: true, message: '请输入活动名称', trigger: 'change'},
        ],
        stocknum: [
          {required: true, message: '请输入数量', trigger: 'blur'}
        ],
        endtime: [
          {required: true, message: '请输入失效时间', trigger: 'blur'}
        ],
        begintime: [
          {required: true, message: '请输入生效时间', trigger: 'blur'}
        ],
        giftname: [
          {required: true, message: '请输入礼品名称', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      // formName.begintime=this.ruleForm.begintime
      alert(this.ruleForm.begintime)
      this.$refs[formName].validate((valid) => {
        gift.add(this.ruleForm).then(res => {
          console.log(res)
          if (res.data == true) {
            this.$message({
              type: 'success',
              message: '添加成功!',
            });
          } else {
            this.$message({
              type: 'defeated',
              message: '添加失败!',
            });
          }
        })
      });
    }
  }
}
</script>